package com.example.unisys.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.*;
import com.example.unisys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/apis/v1/shift/courses", produces = "application/json; charset=utf-8")
public class ShfitController {

    @Autowired
    SubCourseService subCourseService;

    @Autowired
    MainCourseService mainCourseService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseSaleService courseSaleService;

    @Autowired
    TagService tagService;

    /*
     * 获取上架课程
     * */
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public Response getShiftCourse(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText, @RequestParam("sub_school") String sub_school, @RequestParam("main_school") String main_school) {
        HashMap<String, Object> res = new HashMap<>();
        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        if (main_school.equals(sub_school)) {
            //List<SubCourse> ls = subCourseService.getBySchool(main_school,sub_school,newlimit,pagesize);
            //int sum = subCourseService.getBySchoolSum(main_school,sub_school);
            List<MainCourse> ls = mainCourseService.getByMainSchool(main_school, newlimit, pagesize);
            int sum = mainCourseService.getAllCourse(main_school);
            return Response.builder().data(ls).total(sum).status(STATUS.SUCCESS).build();
//            res.put("code", 20000);
//            res.put("data", ls);
//            res.put("total", sum);
        } else {
            List<SubCourse> ls = subCourseService.getBySchool(main_school, sub_school, newlimit, pagesize);
            int sum = subCourseService.getCourseSum(main_school, sub_school);
//            res.put("code",20000);
//            res.put("data",ls);
//            res.put("total",sum);
            return Response.builder().data(ls).total(sum).status(STATUS.SUCCESS).build();
        }
    }

    /*
     * 获取单个课程信息*/
    @RequestMapping(value = "/{course_id}", method = {RequestMethod.GET})
    public Response getShiftCourseById(@PathVariable("course_id") String course_id, @RequestParam("main_school") String main_school, @RequestParam("sub_school") String sub_school) {
        //SubCourse sub = subCourseService.getById(main_school,sub_school,course_id);
        MainCourse mainCourse = mainCourseService.getById(main_school, course_id);
        return Response.builder().status(STATUS.SUCCESS).data(mainCourse).build();
    }


    /*
     * 上架课程
     * */
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public Response shiftCourse(@RequestBody JSONObject jsonParam) {
        MainCourse mainCourse = JSONObject.parseObject(jsonParam.toJSONString(), MainCourse.class);
        String course_id = mainCourse.getCourse_id();
        Course course = courseService.getById(course_id);
//        maincourse.setCourse_brief(course.getCourse_brief());
//        maincourse.setCourse_name(course.getCourse_name());
//        maincourse.setIs_valid(1);
//        maincourse.setMaterial_cover(course.getMaterial_cover());
//        maincourse.setMaterial_name(course.getMaterial_name());
//        maincourse.setNorm_duration(course.getNorm_duration());
//        maincourse.setNorm_sum(course.getNorm_sum());
//        maincourse.setQue_sum(course.getQue_sum());
        List<CourseSale> cs = courseSaleService.getById(mainCourse.getMain_school(), mainCourse.getCourse_id());
        if (cs.size() == 0) {
            //无销量新建
            CourseSale coursesale = CourseSale.builder().build();
            coursesale.setCourse_id(mainCourse.getCourse_id());
//            coursesale.setCourse_name(mainCourse.getCourse_name());
            coursesale.setMain_school(mainCourse.getMain_school());
            coursesale.setSales(0);
            coursesale.setSub_school(mainCourse.getMain_school());
            Tag tag = tagService.getActiveTag();
            coursesale.setTag(tag.getTag_name());
            courseSaleService.insertSelective(coursesale);
        }

        if (mainCourseService.insert(mainCourse) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();

    }

    /*
     * 下架课程
     * */
    @RequestMapping(value = "/{course_id}", method = {RequestMethod.DELETE})
    public Response unshiftCourse(@PathVariable("course_id") String course_id, @RequestBody JSONObject jsonObject) {
        if (mainCourseService.delete(course_id, jsonObject.getString("main_school").trim()) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
     * 更新上架课程
     * */
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public Response updateShiftCourse(@RequestBody JSONObject jsonParam) {
        MainCourse maincourse = JSONObject.parseObject(jsonParam.toJSONString(), MainCourse.class);
        if (mainCourseService.update(maincourse) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

}
